//
//  SettingViewController.swift
//  GreenDeal
//
//  Created by Moennig Q on 9/12/2022.
//

import UIKit
import DaisyNet
class SettingViewController: ModelBaseViewController {
    let customView = SettingView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
        self.initData()
    }
    private func initUI(){
        self.view = customView
        self.customView.setTitle(R.string.localizable.settings())
        self.customView.closeBtn.addTarget(self, action: #selector(dismissVC), for: UIControl.Event.touchUpInside)
       
        //删除账号
        self.customView.deleteAccountClosure =  {[weak self] in
            guard let strongSelf = self else { return  }
            let vc = DeleteAccountVC()
            vc.hidesBottomBarWhenPushed = true
            strongSelf.navigationController?.pushViewController(vc, animated: false)
        }
        //清除缓存
        self.customView.clearCacheClosure =  {[weak self] in
            guard let strongSelf = self else { return  }
            URLCache.shared.removeAllCachedResponses()
            DaisyNet.removeAllCache { flag in
                if let index = strongSelf.customView.list.firstIndex(where: {$0.menuType == .clearCache}) {
                    strongSelf.customView.list[index].content = "0M"
                    strongSelf.customView.tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: UITableView.RowAnimation.none)
                }
                DebugLog("删除所有缓存：" + (flag ? "成功" : "失败"))
            }
        }
        //更新版本
        self.customView.latestVersionClosure =  {
            if let url = URL(string: APIConfig.AppUpgradeAPI){
                UIApplication.shared.open(url)
            }
        }
        //修改密码
        self.customView.changePwdClosure =  {[weak self] in
            guard let strongSelf = self else { return  }
            let vc = ResetPasswordViewController()
            vc.hidesBottomBarWhenPushed = true
            strongSelf.navigationController?.pushViewController(vc, animated: true)
        }
        // Privte Policy
        self.customView.privacyPolicyClosure =  {
            guard let url = URL(string: APPConfig.serverAddress + APIConfig.LoginprivacyPolicy) else { return  }
//            if UIApplication.shared.canOpenURL(url){
//                UIApplication.shared.open(url, options: [:], completionHandler: nil)
//            }
            
            if let naviagtionVC =  self.navigationController {
                let vc = AttachmentViewController()
                vc.attchmentType = .OtherLink
                vc.attachmentURL = url.absoluteString
                vc.attachmentTitle = R.string.localizable.privacyPolicy()
                naviagtionVC.pushViewController(vc, animated: false)
            }
        }
        //terms conditian
        self.customView.termsAndConditionClosure =  {
            guard let url = URL(string: APPConfig.serverAddress + APIConfig.SettingTermsAndCondition) else { return  }
//            if UIApplication.shared.canOpenURL(url){
//                UIApplication.shared.open(url, options: [:], completionHandler: nil)
//            }
            if let naviagtionVC =  self.navigationController {
                let vc = AttachmentViewController()
                vc.attchmentType = .OtherLink
                vc.attachmentURL = url.absoluteString
                vc.attachmentTitle = R.string.localizable.termsAndConditions()
                naviagtionVC.pushViewController(vc, animated: false)
            }
        }
        //login method
        self.customView.loginMethodClosure =  {[weak self] in
            guard let strongSelf = self else { return  }
            let vc = LoginMethodSettingViewController()
            vc.hidesBottomBarWhenPushed = true
            strongSelf.navigationController?.pushViewController(vc, animated: false)
        }
    }
    private func initData(){
        var list = [SettingViewModel]()
        //security
        list.append(SettingViewModel(cellType: .header, title: "SECURITY"))
        list.append(SettingViewModel(cellType: .cell, menuType: .password, title: "Change Password"))
        list.append(SettingViewModel(cellType: .cell, menuType: .loginMethod, title: "Login Method"))
        list.append(SettingViewModel(cellType: .cell, menuType: .displayZoom))
        
        list.append(SettingViewModel(cellType: .header, title: "ACCOUNT"))
        list.append(SettingViewModel(cellType: .cell, menuType: .deleteAccount,title: "Delete Account"))
        
        list.append(SettingViewModel(cellType: .header, title: "INFORMATION"))
        list.append(SettingViewModel(cellType: .cell,menuType: .privacyPolicy, title: R.string.localizable.privacyPolicy()))
        list.append(SettingViewModel(cellType: .cell,menuType: .termsAndCondition, title: R.string.localizable.termsAndConditions()))
        
        list.append(SettingViewModel(cellType: .header, title: "OTHER"))
        list.append(SettingViewModel(cellType: .cell,menuType: .versionInfo, title: APPConfig.loginInfo.appVersionInfo))
        //cache
        let cacheSize: CGFloat = (CGFloat(URLCache.shared.currentDiskUsage + URLCache.shared.currentMemoryUsage) / (1024 * 1024))
        let casheSizeStr = NSString(format: "%.0f", cacheSize)
        list.append(SettingViewModel(cellType: .cell,menuType: .clearCache, title: "Clear Cache",content: "\(casheSizeStr)M"))
        list.append(SettingViewModel(cellType: .cell,menuType: .logOut))
        self.customView.list = list
        self.customView.tableView.reloadData()
    }
}
